package com.eascs.web.o2o.web.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.dao.sql.ObjectSqlBuilder;
import com.eascs.web.o2o.web.dao.BidmDao;
import com.eascs.web.o2o.web.form.LoanBeforeForm;
import com.eascs.web.o2o.web.service.ReportForLoanBeforeService;

@Service
public class ReportForLoanBeforeServiceImpl implements ReportForLoanBeforeService{
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private BidmDao bidmDao;

	@Override
	public PageResult<Object[]> loanBefore(LoanBeforeForm entity) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<Object[]>(Object[].class, entity.getPageSize(), entity.getShowPage());
		ObjectSqlBuilder objectSqlBuilder = ObjectSqlBuilder.create(entity, ConfigContext.getStringValue("sql.ReportForLoanBeforeServiceImpl.loanBefore"));
		sqlPageQuery.setQuerySql(objectSqlBuilder.getSql());
		sqlPageQuery.addParam(entity.getCreateDateStart());
		sqlPageQuery.addParam(entity.getCreateDateEnd());
		if(entity.getBrandName() != null) {
			sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and brand_name like ? ");
			sqlPageQuery.addParam("%"+entity.getBrandName()+"%");
		}
		if(entity.getSsBankName() != null) {
			sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and ss_bank_code = ? ");
			sqlPageQuery.addParam(entity.getSsBankName());
		}
		if(entity.getZxBankName() != null) {
			sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and zx_bank_code = ?");
			sqlPageQuery.addParam(entity.getZxBankName());
		}
		if(entity.getProvinceName() != null) {
			sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and province_name = ?");
			sqlPageQuery.addParam(entity.getProvinceName());
		}
		if(entity.getNsStartDateStart() != null) {
			sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and ns_start_date >= ?");
			sqlPageQuery.addParam(entity.getNsStartDateStart());
		}
		if(entity.getNsStartDateEnd() != null) {
			sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and ns_start_date <= ?");
			sqlPageQuery.addParam(entity.getNsStartDateEnd());
		}
		sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" order by create_date DESC");
		return bidmDao.findDataWithPage(sqlPageQuery);
	}

}
